﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;
using InHD.utilities;
using InHD.Properties;

namespace InHD.model
{
    class InvoiceTemplate : IDataErrorInfo
    {
        #region Property
        public int Id { get; set; }
        public string ArchiveFlag { get; set; }
        public string InvoiceUNK { get; set; }
        public string Content { get; set; }
        public DateTime Modified { get; set; }
        public int ModifiedBy { get; set; }
        public string Name { get; set; }
        public TransTypes TransType { get; set; }
        public string Code { get; set; }
        public byte[] RealContent {get; set;}

        public static Dictionary<string, string> Fields
        {
            get
            {
                Dictionary<string, string> fields = new Dictionary<string, string>();
                fields.Add("Name", "Tên");
                fields.Add("Code", "Mã");
                return fields;
            }
        }

        #endregion

        #region Creation
        protected InvoiceTemplate()
        {
        }

        public static InvoiceTemplate CreateNewInvoiceTemplate()
        {
            return new InvoiceTemplate();
        }

        public static InvoiceTemplate CreateNewInvoiceTemplate(int id, string invoiceUNK, string content, string name, DateTime modified, int modifiedBy, TransTypes transTypes, string code, string archiveFlag)
        {
            return new InvoiceTemplate()
            {
                Id = id,
                InvoiceUNK = invoiceUNK,
                Content = content,
                Name = name,
                ModifiedBy = modifiedBy,
                Modified = modified,
                TransType = transTypes,
                Code = code,
                ArchiveFlag = archiveFlag
            };
        }

        public static InvoiceTemplate CreateNewInvoiceTemplate(int id, string invoiceUNK, string content, string name, DateTime modified, int modifiedBy, TransTypes transTypes, string code, byte[] realContent, string archiveFlag)
        {
            return new InvoiceTemplate()
            {
                Id = id,
                InvoiceUNK = invoiceUNK,
                Content = content,
                Name = name,
                ModifiedBy = modifiedBy,
                Modified = modified,
                TransType = transTypes,
                Code = code,
                RealContent = realContent,
                ArchiveFlag = archiveFlag
            };
        }

        #endregion

        #region IDataErrorInfo Members

        string IDataErrorInfo.Error { get { return null; } }

        string IDataErrorInfo.this[string propertyName]
        {
            get { return this.GetValidationError(propertyName); }
        }

        #endregion // IDataErrorInfo Members

        #region Validation Methods

        /// <summary>
        /// Returns true if this object has no validation errors.
        /// </summary>
        public bool IsValid
        {
            get
            {
                foreach (string property in ValidatedProperties)
                    if (GetValidationError(property) != null)
                        return false;

                return true;
            }
        }

        static readonly string[] ValidatedProperties = 
        { 
            "Content", 
            "Name", 
            "TransType",
            "Code",
        };

        string GetValidationError(string propertyName)
        {
            if (Array.IndexOf(ValidatedProperties, propertyName) < 0)
                return null;

            string error = null;

            switch (propertyName)
            {
                case "Content":
                    error = this.ValidateContent();
                    break;

                case "Name":
                    error = this.ValidateName();
                    break;

                case "TransType":
                    error = this.ValidateTransType();
                    break;

                case "Code":
                    error = this.ValidateCode();
                    break;

                default:
                    Debug.Fail("Unexpected property being validated on CompanyInfo: " + propertyName);
                    break;
            }

            return error;
        }

        private string ValidateCode()
        {
            if (utilities.ValidatingUtilities.ValidateMissing(this.Code))
            {
                return Resources.InHD_Error_MissingField;
            }
            else
            {
                if (utilities.ValidatingUtilities.ValidateUsername(this.Code))
                {
                    return Resources.InHD_InvalidUsername;
                }
            }
            return null;
        }

        private string ValidateTransType()
        {
            return null;
        }

        private string ValidateName()
        {
            if (utilities.ValidatingUtilities.ValidateMissing(this.Name))
            {
                return Resources.InHD_Error_MissingField;
            }
            return null;
        }

        private string ValidateContent()
        {
            if (utilities.ValidatingUtilities.ValidateMissing(this.Content))
            {
                return Resources.InHD_Error_MissingField;
            }
            return null;
        }
        #endregion
    }
}
